package com.gxa.p2p.controller;

import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.pojo.Admin;
import com.gxa.p2p.service.AdminService;
import com.gxa.p2p.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * 管理员操作接口
 * @author HS
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")


@Api(value = "管理员操作接口")
public class AdminController {

    @Autowired
    private AdminService service=null;



    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value = "手机号",required = true),
            @ApiImplicitParam(name="pwd",value = "密码",required = true),
            @ApiImplicitParam(name="code",value = "验证码",required = true)

    })
    public Map login(@RequestParam("phone") String phone, @RequestParam("pwd") String pwd, @RequestParam("code") String code,
                     @ApiIgnore("session") HttpSession session){

        String sessionPhone = session.getAttribute("phone").toString();
        String sessionCode = session.getAttribute("code").toString();


        JsonResult jsonResult=new JsonResult();
        Admin login = service.login(phone);
        if (phone.equals(sessionPhone)&&code.equals(sessionCode)&&login.getPwd().equals(pwd)){
            //生成token值,并将token和phone存入session
            String token= TokenUtil.getToken();
            session.setAttribute("token",token);
            session.setAttribute("phone",phone);
            //返回前端信息和token信息
            jsonResult.getDataMap().put("token",token);
            jsonResult.setCode("200");
            jsonResult.setData(login);
        }else {
            jsonResult.setCode("100");
        }
        return jsonResult.getDataMap();

    }

    @PostMapping("/code")
    @ApiOperation(value = "获得验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value = "手机号",required = true),
    })
    public Map getCode(@RequestParam("phone") String phone, @ApiIgnore("session") HttpSession session){

        JsonResult jsonResult=new JsonResult();

        if(service.login(phone)!=null){

            //生成验证码
            StringBuilder stringBuilder=new StringBuilder();
            for (int i = 0; i < 6; i++) {
                stringBuilder.append(new Random().nextInt(9) + 1);
            }

            //session中储存code和phone值
            session.setAttribute("code",stringBuilder.toString());
            session.setAttribute("phone",phone);

            //返回给前端验证码
            jsonResult.setCode("200");
            jsonResult.setData(stringBuilder.toString());

        }else {
            jsonResult.setCode("100");
        }
        return jsonResult.getDataMap();

    }


    @PostMapping("/total")
    @ApiOperation(value = "首页数据接口")
    public Map findAllData(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        Map totalBorrow = service.totalBorrow();
        Map totalProfit = service.totalProfit();
        Map totalInvset = service.totalInvset();
        Map totalUser = service.totalUser();
        Map totalToDoList = service.totalToDoList();
        List<Double> totalInvestNearMonth = service.totalInvestNearMonth();
        Map<String, List<Double>> totalOutAndInputMoney = service.totalOutAndInputMoney();

        jsonResult.put("totalBorrow",totalBorrow);
        jsonResult.put("totalProfit",totalProfit);
        jsonResult.put("totalInvset",totalInvset);
        jsonResult.put("totalUser",totalUser);
        jsonResult.put("totalToDoList",totalToDoList);
        jsonResult.put("totalInvestNearMonth",totalInvestNearMonth);
        jsonResult.put("totalOutAndInputMoney",totalOutAndInputMoney);
        return jsonResult.getDataMap();
    }
}
